﻿using System;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.SessionState;
using System.Data;
using DBA.Common;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Model;
using DBA.Web.UI;
using DBA.Bll;


namespace DBA.Web.Teaching
{
    /// <summary>
    /// UploadExcel 的摘要说明
    /// </summary>
    public class UploadExcel : IHttpHandler, IRequiresSessionState
    {

        private const int UploadFileLimit = 2;//上传文件数量限制
        private string _msg = "文件导入成功！";//返回信息
        public void ProcessRequest(HttpContext context)
        {
            int iTotal = context.Request.Files.Count;
            if (iTotal == 0)
            {
                _msg = "没有数据";
            }
            else
            {
                int iCount = 0;
                string fileFolder = "upload/" + DateTime.Now.Year + "year/" + DateTime.Now.Month + "month/";
                string savaFileName = "";
                string randomstring = "";

                for (int i = 0; i < iTotal; i++)
                {
                    HttpPostedFile file = context.Request.Files[i];
                    if (!Directory.Exists(UtilsHelper.GetMapPath(UtilsHelper.ServerUrl + fileFolder)))
                    {
                        Directory.CreateDirectory(UtilsHelper.GetMapPath(UtilsHelper.ServerUrl + fileFolder));
                    }
                    if (file.ContentLength > 0 || !string.IsNullOrEmpty(file.FileName))
                    {
                        randomstring = new Random().Next(50000).ToString();


                        savaFileName = UtilsHelper.GetMapPath(UtilsHelper.ServerUrl + fileFolder) + randomstring + file.FileName.Substring(file.FileName.LastIndexOf('\\') + 1).Replace(" ", "");

                        try
                        {
                            file.SaveAs(savaFileName);
                            DataTable dt = NPOIHelper.ImportDataTable(savaFileName);
                            for (int iRowsCount = 0; iRowsCount < dt.Rows.Count; iRowsCount++)
                            {
                                DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
                                dal.ManagerParameter.ClearParameter();
                                dal.ManagerParameter.AddNewInParameter("Name", dt.Rows[iRowsCount][0], DbType.String, 20);
                                dal.ManagerParameter.AddNewInParameter("CourseName", dt.Rows[iRowsCount][1], DbType.String, 100);
                                dal.ManagerParameter.AddNewInParameter("Period", ConvertHelper.ToInt32(dt.Rows[iRowsCount][2]), DbType.Int32, 4);
                                dal.ManagerParameter.AddNewInParameter("Grade",GetGrade(dt.Rows[iRowsCount][4]), DbType.Int32, 4);
                                dal.ManagerParameter.AddNewInParameter("Demo", dt.Rows[iRowsCount][3], DbType.String, 200);
                                dal.ManagerParameter.AddNewOutParameter("reValue", DbType.Int32);
                                dal.ExecuteStore("Proc_BatchAddTeacherCourse");
                                //string m = dal.ManagerParameter.GetParameter("reValue");
                            }
                        }
                        catch
                        {
                            _msg = "导入中出现错误，请重新尝试!";
                            break;
                        }
                        //这里可以根据实际设置其他限制
                        if (++iCount > UploadFileLimit)
                        {
                            _msg = "超过上传限制：" + UploadFileLimit;
                            break;
                        }
                    }
                }
            }
            context.Response.Write(_msg);
        }
        private int GetGrade(object type)
        {
            switch (type.ToString())
            {
                case "本科":
                    return 0;
                case "普研(含博士)":
                    return 1;
                case "专业学位":
                    return 2;
                case "其他(非64学时)":
                    return 3;
                case "其他":
                    return 4;
            }
            return 4;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}